import React from 'react';
import ReactDOMServer from 'react-dom/server';
import { StaticRouter } from 'react-router-dom';
import { Provider } from 'react-redux';
import { createNewStore as createStore } from '../store'
import Routes from '../routes';

const App = (props) => {
    const { location } = props
    const store = createStore()

    return (
        <Provider store={store}>
            <StaticRouter location={location} context={{}}>
                <Routes />
            </StaticRouter>
        </Provider>
    )
};

export default (req, res) => {
    const appString = ReactDOMServer.renderToString(<App location={req.path} />);

    return `
        <html>
            <head>
                <title>React-ssr</title>
            </head>
            <body>
                <div id="root">${appString}</div>
                <script src="/public/index.js"></script>
            </body>
        </html>`;
};
